package com.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.springform.User;


@Controller
public class MyController {
	
	
	@RequestMapping(value="loginPage")
	public ModelAndView getLoginPage(HttpServletRequest request)
	{
		ModelAndView mav = new ModelAndView("login");
		System.out.println("Inside getLoginPage method");
		String user=request.getParameter("user");
		mav.addObject("user", user);
		return mav;
	}
	
	
	@RequestMapping(value="welcomePage")
	public ModelAndView getWelcomePage(@RequestParam("user") String userName,@RequestParam("pass") String password,HttpServletResponse response) throws IOException
	{
		System.out.println("Inside  getWelcomePage method");
		ModelAndView mav = new ModelAndView("welcome");
		System.out.println(userName);
		response.getWriter().write("welcome "+userName);
		mav.addObject("userName",userName);
		return mav;
	}
	
	
	@RequestMapping(value="registerViaSpringForm")
	public ModelAndView getRegistrationPage()
	{
		ModelAndView mav = new ModelAndView("springForm");
		System.out.println("Inside getRegistrationPage method");
		mav.addObject("user",new User());
		return mav;
	}

	@RequestMapping(value="submitDetails")
	public ModelAndView getSuccessPage(@ModelAttribute User user,BindingResult result)
	{
		ModelAndView mav = new ModelAndView("success");
		System.out.println("Inside getSuccessPage method");
		System.out.println("Hey "+user+" you are successfully registered!!");
		return mav;
	}

}
